#coding=utf-8
'''
Created on 2011-1-20

@author: zarra
'''
from sqlalchemy import  Column, Integer, String,Unicode,UnicodeText,Text,ForeignKey
from sqlalchemy.orm import relation, backref,eagerload,sessionmaker,scoped_session
from model.fghisdb._base import Base,fghisdb
import json
class Doctor_Advice(Base):
    '''
        医嘱
    '''
    __tablename__=u'住院_医嘱明细表'
    id = Column(u'医嘱编号',String(16),primary_key=True)
    fid=Column(u'相关医嘱',String(16))
    type=Column(u'医嘱种类',Unicode(2),nullable=False)
    patient_id=Column(u'住院号',String(6),nullable=False)
    doctor_id = Column(u'开方医生',String(4),nullable=False)
    title = Column(u'名称',Unicode(256),nullable=False)
    drug_id = Column( u'代码',String(16),ForeignKey(u'代码_药品信息表.药品代码'),nullable=False )
    #drug = relation("Drug_Info")
    
    drug_types = [
                  u'草药'.encode('cp936'),
                  u'成药'.encode('cp936'),
                  u'西药'.encode('cp936'),
                  ]
    
    def __repr__(self):
        return json.dumps(self.__json__())
    
    def __json__(self):
        return {'id':self.id, 
                'fid':self.fid,
                'type':self.type,
                'patient_id':self.patient_id,
                'doctor_id':self.doctor_id,
                'drug_id':self.drug_id,
                'title':self.title,
                #'input_code':self.drug.input_code
                }
        
class Drug_Info(Base):
    '''
        药品信息
    '''
    __tablename__=u'代码_药品信息表'
    id = Column(u'药品代码',String(16),primary_key=True)
    input_code=Column(u'输入码',String(16))
    normal_input_code=Column(u'通用输入码',String(16))
    type=Column(u'类型',Integer,nullable=False)
    factory = Column(u'产地',Unicode(256))
    pihao = Column(u'批准文号',Unicode(256))
    title = Column(u'药品名称',Unicode(256),nullable=False)
    normal_title = Column(u'通用名称',Unicode(256))
    def __repr__(self):
        return json.dumps(self.__json__())
    
    def __json__(self):
        return {'id':self.id, 
                'input_code':self.input_code,
                'normal_input_code':self.normal_input_code,
                'type':self.type,
                'title':self.title,
                'normal_title':self.normal_title,
                'pihao':self.pihao,
                'factory':self.factory,
                }
    
if __name__ == '__main__':
    s=fghisdb()
    a=s.query(Doctor_Advice)\
        .filter(Doctor_Advice.type.in_(Doctor_Advice.drug_types) )\
        .first()
    

    print a
    

        